import {DataStore} from "./DataStore.js";
import {DeterMineData} from "./DeterMineData.js";
import {AUTHORIZELOGIN} from "./ConstUrl.js";

export class AuthorizeLogin extends DeterMineData {
    constructor(game_id, game_auth_code, outer_name) {
        super(game_id, game_auth_code);
        this.game_id = game_id;
        this.game_auth_code = game_auth_code;
        this.outer_name = outer_name;
    }

    authorizeLogin(successCallback = super.request, failsCallback = super.request, outer_name) {
        this.dataStore.delete('loginCode');
        super.deterMineData(failsCallback, () => {
            this.loginServer(successCallback, failsCallback, outer_name);
        });
    }

    loginServer(successCallback, failsCallback) {

        const userinfo = this.dataStore.get('userInfo');
        const systemInfo = this.dataStore.get('systemInfo');
        const networkType = this.dataStore.get('networkType');
        const loginCode = this.dataStore.get('loginCode');

        const params = {
            auth: '1',
            code: loginCode.code,
            iv: userinfo.iv,
            encryptedData: userinfo.encryptedData,
            rawData: userinfo.rawData,
            signature: userinfo.signature,
            brand: systemInfo.brand,
            model: systemInfo.model,
            version: systemInfo.version,
            system: systemInfo.system,
            platform: systemInfo.platform,
            network: networkType.networkType,
            game_id: this.game_id,
            lm: this.dataStore.get('lm'),
        };
        if (this.outer_name !== undefined) {
            params.outer_name = this.outer_name;
        }
        super.requestServer(successCallback, failsCallback, AUTHORIZELOGIN, params)
    }
}